# $Id: Bounce.pm 36 2006-09-29 23:09:30Z cmauch $
#
# Author: Charles Mauch (cmauch@gmail.com)
# Description: Module to handle email bouncing if needed.
# Legal: (c) 2006 Charles Mauch, released under the terms of the GPLv2 or later

# -- [ sub: BounceCheck ] ---------------------------------------------------
#
#Summary:      Subroutine to check emails for validity, and call a sub to
#              bounce them if necessary.
#Parameters:   email address, full name, subject of original email
#Return:       none

sub BounceCheck {
    my ( $address, $username, $subject ) = @_;

    if ( $subject eq "" ) {    # Blank subjects, bounce em back to resend
        BounceMessage( "nosubject", "$address", "$username", "$subject" );
        Logger( $epochtime, $score, "other", $username, $address, $subject, "admin\/nosubject" );
        $item->accept( "$MauchConfig::maildir/admin/nosubject" );
    }

    # Blank Message bodies, bounce em back asking what the email was about
    #
    # Doesn't seem to work with mime messages, which is very bad.
    #
    # if (($msgbody eq "\n") || ($msgbody eq "") {
    #  bouncemessage("nobody");
    #  $item->accept("$MauchConfig::maildir/admin/nobody")
    # }
} ## end sub BounceCheck

# -- [ sub: BounceMessage ] -------------------------------------------------
#
#Summary:      Subroutine to actually bounce an email message
#Parameters:   bounce reason, email address, full name, subject of original email
#Return:       none

sub BounceMessage {
    use Mail::Sendmail;
    my ( $bouncereason, $address, $username, $subject ) = @_;

    if ( $bouncereason eq "nosubject" ) {
        my %mail = (
            To         => $username . '<' . $address . '>',
            From       => $MauchConfig::myname . ' Helper <' . $MauchConfig::bounceaddress . '>',
            Subject    => 'No subject!',
            'X-Mailer' => "Mail::Sendmail version $Mail::Sendmail::VERSION",
            'X-Loop'   => 'nosubjectresponder',
            smtp       => $MauchConfig::smtpserver,
            Message    => << "EOF"
From: Charles Mauch's Helper Software

This is an automated response and does not indicate that your 
message has been read."

You just sent me an email has no subject.  I generally mark these 
messages as spam or junkmail - so you should probably resend your
message with a proper subject line.   

Thank you for your patience!

-- Charles

Your original message text is:

$item->body();

EOF
                   );
        $mail{ Date } = Mail::Sendmail::time_to_date( time() - 86400 );
        sendmail( %mail );    # Even if failure, don't die!
    } ## end if ( $bouncereason eq ...
} ## end sub BounceMessage

1;
